package com.ztesoft.web.taskgenerator.taskjob;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ztesoft.framework.exception.BaseAppException;
import com.ztesoft.framework.log.ZTEsoftLogManager;
import com.ztesoft.framework.util.DateUtils;
import com.ztesoft.framework.util.Utils;
import com.ztesoft.web.baseconfig.db.po.DmPlanPO;
import com.ztesoft.web.baseconfig.db.po.DmPlanRelationPO;
import com.ztesoft.web.baseconfig.service.IDmPlanRelationService;
import com.ztesoft.web.baseconfig.service.IDmPlanService;
import com.ztesoft.web.common.DMSConstant;
import com.ztesoft.web.taskgenerator.db.po.DmTaskPO;
import com.ztesoft.web.taskgenerator.service.IDmTaskService;



@Service
public class DmHandTask {

	@Autowired
	private IDmPlanRelationService iDmPlanRelationService;
	
	@Autowired
	private IDmPlanService iDmPlanService;
	
	@Autowired
	private IDmTaskService iDmTaskService;
	
	/**
	 * 日志
	 */
    private final static ZTEsoftLogManager LOGGER = ZTEsoftLogManager
            .getLogger(DmHandTask.class);
    

	public String initTableName(int dmPlanId, List<DmPlanRelationPO> dmPlanRelationLst) {
		List<DmTaskPO> dtLst = new ArrayList<DmTaskPO>();//构造List存放批量新增的任务实例
		//List<DmPlanRelationPO> dmPlanRelationLst =  new ArrayList<DmPlanRelationPO>();
		Integer groupNbr;
		String result = "";
		DmPlanPO dmPlan;
		try {
			dmPlan = iDmPlanService.selectByPrimaryKey(dmPlanId);
			if(Utils.isEmpty(dmPlanRelationLst)){
				dmPlanRelationLst = iDmPlanRelationService.selectByDmPlanId(dmPlanId);
			}
			LOGGER.info("手动任务执行ing||start..dmPlanRelationLst:", dmPlanRelationLst);
			if(dmPlanRelationLst!=null && dmPlanRelationLst.size()>0){
				//循环每条取得的记录
				for (DmPlanRelationPO dmPlanRelationPO : dmPlanRelationLst) {
					groupNbr = dmPlanRelationPO.getGroupNbr();
//					/*2.2）	使用DM_PLAN_ID、GROUP_NBR查询DM_TASK，查看是否有记录*/
//					Integer count = iDmTaskService.countByArg(dmPlanId, groupNbr);
//					LOGGER.info("手动任务执行ing..groupNbr:", groupNbr+"count:"+count);
//					if(count!=null && count>0){
//						result = "该计划【"+dmPlan.getPlanName()+"】的为手动执行生成任务,无下周期任务!;";
//						break;
//					}else{ /// modified by lwb 2015/01/05 
						DmTaskPO dmTaskPO = new DmTaskPO();
						dmTaskPO.setDmPlanId(dmPlanId);
						dmTaskPO.setGroupNbr(groupNbr);
						dmTaskPO.setSourceType(dmPlan.getSourceType());//源数据类型
						dmTaskPO.setName(getTaskName(dmPlan,groupNbr));//根据周期来计算
						dmTaskPO.setPriority(getMapInfo(dmPlan).get("priority"));
						dmTaskPO.setScheduleDate(new Date());
						dmTaskPO.setIsCheck(getMapInfo(dmPlan).get("needCheck"));//是否需要审核
						dmTaskPO.setNodeId(dmPlan.getNodeId());
						dmTaskPO.setOperType(dmPlan.getOperType());
						dtLst.add(dmTaskPO);

//					}
				}
			}
			//批量更新任务
			if(!Utils.isEmpty(dtLst)){
				iDmTaskService.addBatch(dtLst);
				LOGGER.info("手动任务执行ing||end..批量更新列表dtLst:", dtLst);
				result ="计划【"+dmPlan.getPlanName()+"】启动成功，任务已生成!;";
			}
		} catch (BaseAppException e) {
			e.printStackTrace();
		}finally{
			dmPlanRelationLst.clear();
		}
		return result;
	}

		/**
		 * //任务名称根据计划名称+日期+组号
		 * @param dmPlan
		 * @return
		 */
		private String getTaskName(DmPlanPO dmPlan, Integer groupNbr){
			SimpleDateFormat sdf =  new SimpleDateFormat(DateUtils.STR_DATE_FORMAT_DAY_WITHOUT_SPLIT);//yyyy-MM 
			StringBuffer sbName = new StringBuffer();
			sbName.append(dmPlan.getPlanName()).append("-").append(sdf.format(new Date())).append("-").append(groupNbr);
			return sbName.toString();

		}
		private Map<String, Integer> getMapInfo(DmPlanPO dmPlan){
			Map<String, Integer> map = new HashMap<String, Integer>();
			Integer priority;
			if(DMSConstant.EXEC_TYPE_AUTO.equals(dmPlan.getExecType())){//若计划为自动执行，则填为2；
				priority = DMSConstant.PRIORITY_AUTO;
			}else{//若计划为手工执行，则填为1；
				priority = DMSConstant.PRIORITY_HAND;
			}
			Integer needCheck;
			//是否审核通过
			if("F".equalsIgnoreCase(dmPlan.getNeedCheck())||dmPlan.getOperType()==2){//T需要确认==待审核,F不需要确认==审核通过
				needCheck = DMSConstant.IS_CHECK_PASS;
			}else{
				needCheck = DMSConstant.IS_CHECK_DEALPASS;
			}
			map.put("priority", priority);
			map.put("needCheck", needCheck);
			
			return map;
		}
}
